package com.haogu.service.impl;

import com.haogu.entity.Dict;
import com.haogu.entity.DictType;
import com.haogu.entity.VDict;
import com.haogu.mapper.DictMapper;
import com.haogu.mapper.DictTypeMapper;
import com.haogu.service.DictService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class DictServiceImpl implements DictService {

    @Resource
    private DictMapper dictMapper;

    @Resource
    private DictTypeMapper dictTypeMapper;

    /**
     * 展示字典分页
     * @param page
     * @param rows
     * @param typeId
     * @return
     */
    @Override
    public Map selectDictByPage(Integer page, Integer rows, Integer typeId) {
        List<VDict> vDicts = dictMapper.selectDictByPage((page - 1) * rows, rows, typeId);
        Integer integer = dictMapper.selectCount(null);
        Map map = new HashMap();
        map.put("result",vDicts);
        map.put("totalRows",integer);
        Map map1 = new HashMap();
        map1.put("data",map);
        return map1;
    }

    /**
     * 去添加页面的回显数据
     */
    public void toAddDict(Model model){
        List<DictType> dictTypes = dictTypeMapper.selectList(null);
        model.addAttribute("dictTypes",dictTypes);
    }
    /**
     * 添加dict
     */
    public Map addDict(Dict dict){
        Map map = new HashMap();
        int insert = dictMapper.insert(dict);
        System.out.println(insert+"1111111111111111");
        if(insert==0){
            map.put("success",false);
        }else{
            map.put("success",true);
        }
        return map;
    }
    /**
     * 去修改页面
     */
    public void toUpdateDict(Integer id, Model model){
        Dict dict = dictMapper.selectById(id);
        List<DictType> dictTypes = dictTypeMapper.selectList(null);
        model.addAttribute("dictTypes",dictTypes);
        model.addAttribute("dict",dict);
    }
    /**
     * 修改DIct
     */
    public Map updateDict(Dict dict){
        Map map = new HashMap();
        int i = dictMapper.updateById(dict);
        if(i==0){
            map.put("success",false);
        }else{
            map.put("success",true);
        }
        return map;
    }

    /**
     * 删除dict
     * @param ids
     * @return
     */
    public Map deleteDict(String ids){
        //对传过来的ids进行操作
        String[] split = ids.split(",");
        System.out.println(ids);
        System.out.println(split);
        boolean flag = true;
        for(int i = 0; i < split.length;i++){

            int i1 = dictMapper.deleteById(Integer.parseInt(split[i]));
//            boolean b = leadsService.deleteLeadsById(Integer.parseInt(split[i]));
            if(i1==0){
                flag = false;
            }
        }
        Map map = new HashMap();
        if(flag){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败");
        }
        return map;
    }
}
